package com.scc.spring;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;

import com.scc.entites.dto.DishEntityDTO;
import com.scc.services.validation.IServiceValidator;


@Aspect
public class ValidationAspect
{
   @Autowired
   IServiceValidator serviceValidator;
   
   @Pointcut("within(com.scc.services.DishSetupService)")
   private void services() {}
   
   @Pointcut("execution(public * *(..))")
   private void anyPublicOperation() {}
   
   @Around("services() && @annotation(com.scc.spring.Validational)")
   public Object requestValidationAdvise(ProceedingJoinPoint pjp) throws Throwable {
     
     DishEntityDTO incomingDTO = getIncomingDTO(pjp.getArgs());
     validator(incomingDTO);
     Object retVal = pjp.proceed();
     
     return retVal;
   }
   
   public void validator(DishEntityDTO incomingDTO)
   {
      serviceValidator.validateDTO(incomingDTO);
   }
   
   private DishEntityDTO getIncomingDTO(Object[] args)
   {
      DishEntityDTO res = (DishEntityDTO) args[0];
      return res;
   }

   public IServiceValidator getServiceValidator()
   {
      return serviceValidator;
   }

   public void setServiceValidator(IServiceValidator serviceValidator)
   {
      this.serviceValidator = serviceValidator;
   }
}
